package part1.api.collection;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

public class CollectionToArray {
    public static void main(String[] args) {

        Collection<String> c = new ArrayList<>();
        c.add("one");
        c.add("two");
        c.add("three");
        c.add("four");
        c.add("five");
        System.out.println(c); //[one, two, three, four, five]

        //若参数数组元素个数==集合元素个数，那就正常转换
        //若参数数组元素个数<集合元素个数，那也正常转换(按照集合大小给数组)
        //若参数数组元素个数>集合元素个数，那也正常转换，同时在末尾补默认值
        String[] arry = c.toArray(new String[3]);
        System.out.println(Arrays.toString(arry));//[one, two, three, four, five]
        arry[0] = "six";
        System.out.println(c);//[one, two, three, four, five]
        c.add("six");
        System.out.println(Arrays.toString(arry));//[six, two, three, four, five]
        /*
        集合转数组后，双方修改互不影响，一方修改另一方不会跟着修改
         */
    }
}
